/*
 *    Copyright (c) 2018-2025, boss All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the boss developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: boss
 */

package com.hopes.bizz.poll.om.controller;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hopes.bizz.poll.common.constant.MonitorConstant;
import com.hopes.bizz.poll.om.dto.PollRtDataDTO;
import com.hopes.bizz.poll.om.dto.PollUploadDataDTO;
import com.hopes.bizz.poll.om.entity.PollRtDataEntity;
import com.hopes.bizz.poll.om.service.PollDayService;
import com.hopes.bizz.poll.om.service.PollHourService;
import com.hopes.bizz.poll.om.service.PollMinService;
import com.hopes.bizz.poll.om.service.PollRtDataService;
import com.hopes.bizz.poll.om.vo.PollAnalysisDataVO;
import com.hopes.bizz.poll.town.service.PollTownRtDataService;
import com.hopes.boss.common.core.util.R;
import com.hopes.boss.common.excel.annotation.ResponseExcel;
import com.hopes.boss.common.log.annotation.SysLog;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpHeaders;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static com.hopes.bizz.poll.common.constant.OmConstant.*;
import static org.bouncycastle.asn1.cmc.CMCStatus.failed;
import static org.bouncycastle.asn1.cmc.CMCStatus.success;


/**
 * 实时监控数据
 *
 * @author ljh
 * @date 2023-05-22 17:05:57
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/rtData")
@Tag(description = "rtData", name = "实时监控数据管理")
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class PollRtDataController {

    private final PollRtDataService pollRtDataService;
    private final PollTownRtDataService pollTownRtDataService;
    private final PollDayService pollDayService;
    private final PollHourService pollHourService;
    private final PollMinService pollMinService;


    /**
     * 实时数据趋势分析
     *
     * @param pollRtDataDTO 查询条件
     * @return
     */
    @Operation(summary = "实时数据趋势分析", description = "实时数据趋势分析")
    @GetMapping("/trendAnalysis")
    @PreAuthorize("@pms.hasPermission('poll_rtData_view')")
    public R trendAnalysis(PollRtDataDTO pollRtDataDTO) {

        PollAnalysisDataVO pollAnalysisDataVO = pollRtDataService.trendAnalysis(pollRtDataDTO);

        return R.ok(pollAnalysisDataVO);
    }


    /**
     * 分页查询
     *
     * @param page       分页对象
     * @param pollRtData 实时监控数据
     * @return
     */
    @Operation(summary = "分页查询", description = "分页查询")
    @GetMapping("/page")
    @PreAuthorize("@pms.hasPermission('poll_rtData_view')")
    public R getPollRtDataPage(Page page, PollRtDataDTO pollRtData) {
        return R.ok(pollRtDataService.queryByMonitorAndTimeType(page, pollRtData));
    }


    /**
     * 通过monitorSiteId查询实时监控数据
     *
     * @param  monitorSiteId
     * @return R
     */
    @Operation(summary = "通过monitorSiteId查询", description = "通过monitorSiteId查询")
    @GetMapping("/{monitorSiteId}")
    @PreAuthorize("@pms.hasPermission('poll_rtData_view')")
    public R getById(@PathVariable("monitorSiteId") Long monitorSiteId) {
        List<PollRtDataEntity> pollRtDataList = pollRtDataService.list(new QueryWrapper<PollRtDataEntity>().eq("monitor_site_id",monitorSiteId));
        return R.ok(pollRtDataList);
    }
    /**
     * AQI排名查询
     *
     * @return
     */
    @Operation(summary = "AQI排名查询", description = "AQI排名查询")
    @GetMapping("/aqiSort/{type}")
    @PreAuthorize("@pms.hasPermission('poll_rtData_view')")
    public R aqiSort(@PathVariable String type) {
        if (MonitorConstant.TOWN.equals(type)) {
            return R.ok(pollTownRtDataService.getAqiSort());
        } else if (MonitorConstant.AIR_MONITOR.equals(type)) {
            return R.ok(pollRtDataService.getAqiSort());
        }
        return R.failed();
    }

    /**
     * WQI排名查询
     *
     * @return
     */
    @Operation(summary = "WQI排名查询", description = "WQI排名查询")
    @GetMapping("/wqiSort/{monitorType}")
    @PreAuthorize("@pms.hasPermission('poll_rtData_view')")
    public R wqiSort(@PathVariable Integer monitorType) {
            return R.ok(pollRtDataService.getWqiSort(monitorType));
    }


    /**
     * 新增实时监控数据
     *
     * @param pollRtData 实时监控数据
     * @return R
     */
    @Operation(summary = "新增实时监控数据", description = "新增实时监控数据")
    @SysLog("新增实时监控数据")
    @PostMapping
    @PreAuthorize("@pms.hasPermission('poll_rtData_add')")
    public R save(@RequestBody PollRtDataEntity pollRtData) {
        return R.ok(pollRtDataService.save(pollRtData));
    }

    /**
     * 修改实时监控数据
     *
     * @param pollRtData 实时监控数据
     * @return R
     */
    @Operation(summary = "修改实时监控数据", description = "修改实时监控数据")
    @SysLog("修改实时监控数据")
    @PutMapping
    @PreAuthorize("@pms.hasPermission('poll_rtData_edit')")
    public R updateById(@RequestBody PollRtDataEntity pollRtData) {
        return R.ok(pollRtDataService.updateById(pollRtData));
    }

    /**
     * 通过id删除实时监控数据
     *
     * @param ids id列表
     * @return R
     */
    @Operation(summary = "通过id删除实时监控数据", description = "通过id删除实时监控数据")
    @SysLog("通过id删除实时监控数据")
    @DeleteMapping
    @PreAuthorize("@pms.hasPermission('poll_rtData_del')")
    public R removeById(@RequestBody Long[] ids) {
        return R.ok(pollRtDataService.removeBatchByIds(CollUtil.toList(ids)));
    }


    /**
     * 手动上传数据
     *
     * @param uploadData 上传数据
     * @return R
     */
    @Operation(summary = "手动上传数据", description = "手动上传数据")
    @SysLog("手动上传数据")
    @PostMapping("/upload")
    public R upload(@RequestBody PollUploadDataDTO uploadData) {
        Integer dataType = uploadData.getDataType();
        List pollUploadDataList = uploadData.getPollUploadDataList();
        if (dataType == DATA_TYPE_RT) {
            pollRtDataService.uploadData(pollUploadDataList);
            return R.ok(success);
        } else if (dataType == DATA_TYPE_MIN) {
            return R.ok(pollMinService.uploadData(pollUploadDataList));
        } else if (dataType == DATA_TYPE_HOUR) {
            return R.ok(pollHourService.uploadData(pollUploadDataList));
        } else if (dataType == DATA_TYPE_DAY) {
            return R.ok(pollDayService.uploadData(pollUploadDataList));
        } else {
            return R.ok(failed, "时间类型错误");
        }
    }

    /**
     * 导出excel 表格
     *
     * @param pollRtData 查询条件
     * @return excel 文件流
     */
    @ResponseExcel
    @GetMapping("/export")
    @PreAuthorize("@pms.hasPermission('poll_rtData_export')")
    public List<PollRtDataEntity> export(PollRtDataEntity pollRtData) {
        return pollRtDataService.list(Wrappers.query(pollRtData));
    }
}
